/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "esp8266.h"
#include "oled.h"
#include "stdio.h"
#include "string.h"
#include "dht11.h"
#include "semphr.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
extern UART_HandleTypeDef huart2;
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
info ttinfo={0};
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
osThreadId_t f1TaskHandle;
osThreadId_t f2TaskHandle;
	SemaphoreHandle_t semhandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

const osThreadAttr_t f1Task_attributes = {
  .name = "f1Task",
  .stack_size = 128 * 4*2,
  .priority = (osPriority_t) osPriorityNormal,
};

const osThreadAttr_t f2Task_attributes = {
  .name = "f2Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void f1task(void*arg);
void f2task(void*arg);
void readbuffer(uint8_t*m);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  semhandle=xSemaphoreCreateBinary();
	xSemaphoreGive(semhandle);

  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
	esp8266_init();
	OLED_Init();
	OLED_Clear();
	dht11_init();
	OLED_ShowString(0,0,(uint8_t*)"[1]INIT OK",16);
	
	if(esp8266_link_wifi(PID,PASSWARD))
	{
	    OLED_ShowString(0,2,(uint8_t*)"[2]CONN OK",16);

	}
	else
	{
	   OLED_ShowString(0,2,(uint8_t*)"[2]CONN ERR",16);
	}
	
	if(esp8266_connect_MQTT(MQTTIP,MQTTPORT,"zjw","zjw"))
	{
	
	  OLED_ShowString(0,4,(uint8_t*)"[3]CONN MQTT OK",16);
	
	
	}
	else
	{
	  OLED_ShowString(0,4,(uint8_t*)"[3]CONN MQTT ERR",16);
	
	}
	
	f1TaskHandle= osThreadNew(f1task, NULL, &f1Task_attributes);
	osDelay(500);
	f2TaskHandle= osThreadNew(f2task, NULL, &f2Task_attributes);
	
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void f1task(void*arg)
{
   if(esp8266_subscribe(TOPIC))
	  OLED_ShowString(0,6,(uint8_t*)"[4]MQTT SUB OK",16);

   else
		OLED_ShowString(0,6,(uint8_t*)"[4]MQTT SUB ERR",16); 

   uint8_t buffer[512]={0};
	 while(1)
	 {
	    readbuffer(buffer);
	    
		 printf("get from server %s\r\n",buffer);
		 
		 memset(buffer,0,sizeof(buffer));

	 }

}
void readbuffer(uint8_t*m)
{    uint8_t i=0;
     uint8_t data,flag=0;
	   uint32_t old_tick=0;
  while (1)
  {
   
	
		if(HAL_GetTick()-old_tick>50&&flag)
		{
		
		   break;
		
		
		}
    if(HAL_UART_Receive(&huart2,(uint8_t*)&data,1,10)==HAL_TIMEOUT)
		{
		 continue;
		
		}
		else
		{	
		  m[i++]=data;
		  old_tick=HAL_GetTick();
			flag=1;
		
		
		
		}
	 
		
  }




}
void f2task(void*arg)
{
   uint8_t data[5]={0}; 
	while(1)
	{
	  vTaskSuspendAll();
	  dht11_send(data);
	  xTaskResumeAll(); 
		xSemaphoreTake(semhandle,portMAX_DELAY);
	  ttinfo.tmp=data[0];
		ttinfo.hum=data[2];
		xSemaphoreGive(semhandle);
	   
	  printf("%d %d\r\n",data[0],data[2]);
		memset(data,0,sizeof(data));
		osDelay(500);
	}

}
/* USER CODE END Application */